package com.micro.ai.files.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.files.entity.StorageConfig;
import com.micro.ai.files.service.StorageConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 存储配置控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/storage-configs")
@Tag(name = "存储配置管理", description = "文件存储配置的增删改查、设置默认等功能")
public class StorageConfigController {

    @Autowired
    private StorageConfigService storageConfigService;

    /**
     * 创建存储配置
     */
    @PostMapping
    @PreAuthorize("hasAuthority('storage:create')")
    @Operation(summary = "创建存储配置", description = "创建新的存储配置（本地、S3、OSS等）")
    public ApiResponse<StorageConfig> create(@Valid @RequestBody StorageConfig config) {
        log.info("创建存储配置请求: tenantId={}, configName={}, provider={}", 
                config.getTenantId(), config.getConfigName(), config.getProvider());
        
        StorageConfig result = storageConfigService.create(config);
        return ApiResponse.success("创建成功", result);
    }

    /**
     * 更新存储配置
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('storage:update')")
    @Operation(summary = "更新存储配置", description = "更新存储配置信息")
    public ApiResponse<StorageConfig> update(
            @Parameter(description = "配置ID", required = true) @PathVariable String id,
            @Valid @RequestBody StorageConfig config) {
        log.info("更新存储配置请求: configId={}", id);
        
        StorageConfig result = storageConfigService.update(id, config);
        return ApiResponse.success("更新成功", result);
    }

    /**
     * 删除存储配置
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('storage:delete')")
    @Operation(summary = "删除存储配置", description = "删除存储配置")
    public ApiResponse<Void> delete(
            @Parameter(description = "配置ID", required = true) @PathVariable String id) {
        log.info("删除存储配置请求: configId={}", id);
        
        storageConfigService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

    /**
     * 获取存储配置详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('storage:view')")
    @Operation(summary = "获取存储配置详情", description = "根据ID获取存储配置详细信息")
    public ApiResponse<StorageConfig> getById(
            @Parameter(description = "配置ID", required = true) @PathVariable String id) {
        StorageConfig config = storageConfigService.getById(id);
        if (config == null) {
            return ApiResponse.error("F0002", "存储配置不存在");
        }
        return ApiResponse.success(config);
    }

    /**
     * 分页查询存储配置
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('storage:view')")
    @Operation(summary = "分页查询存储配置", description = "支持多条件查询存储配置列表")
    public ApiResponse<Page<StorageConfig>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID") @RequestParam(value = "tenantId", required = false) String tenantId,
            @Parameter(description = "关键词") @RequestParam(value = "keyword", required = false) String keyword,
            @Parameter(description = "存储提供商") @RequestParam(value = "provider", required = false) String provider,
            @Parameter(description = "状态") @RequestParam(value = "status", required = false) String status) {
        
        Page<StorageConfig> page = storageConfigService.pageQuery(pageNum, pageSize, tenantId, keyword, provider, status);
        return ApiResponse.success(page);
    }

    /**
     * 设置默认配置
     */
    @PutMapping("/{id}/set-default")
    @PreAuthorize("hasAuthority('storage:set-default')")
    @Operation(summary = "设置默认配置", description = "将指定配置设置为该租户和提供商的默认配置")
    public ApiResponse<Void> setDefault(
            @Parameter(description = "配置ID", required = true) @PathVariable String id) {
        log.info("设置默认存储配置请求: configId={}", id);
        
        storageConfigService.setDefault(id);
        return ApiResponse.successVoid("设置成功");
    }

    /**
     * 获取默认配置
     */
    @GetMapping("/default")
    @PreAuthorize("hasAuthority('storage:view')")
    @Operation(summary = "获取默认配置", description = "获取指定租户和提供商的默认配置")
    public ApiResponse<StorageConfig> getDefault(
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "存储提供商") @RequestParam(value = "provider", required = false) String provider) {
        
        StorageConfig config = storageConfigService.getDefaultConfig(tenantId, provider);
        if (config == null) {
            return ApiResponse.error("F0002", "未找到默认配置");
        }
        return ApiResponse.success(config);
    }
}

